<?php
class CategoriesController extends Controller{

	function index(){

	}

	/**
	* ADMIN
	**/
	function admin_index(){
		$perPage = 10;
		$this->loadModel('Categorie'); 
		$condition = array('');
		$d['categories'] = $this->Categorie->find(array(
			'fields' 	 => 'id,nom',
			'conditions' => $condition,
			'limit' 	 => ($perPage*($this->request->page-1)).','.$perPage,
			));
		$d['total'] = $this->Categorie->findCount($condition);
		$d['page'] = ceil($d['total'] / $perPage);
		$this->set($d);
	}

	/**
	* Permet d'éditer une categorie
	**/
	function admin_edit($id = null){
		$this->loadModel('Categorie');
		$d['id']= '';
		if ($this->request->data){

			$this->Categorie->save($this->request->data);
			$this->Session->setFlash('Le contenu a bien été modifié','alert-success');
			$id = $this->Categorie->id;
			$this->redirect('admin/categories/index');	
		}
		if($id){
			$this->request->data = $this->Categorie->findFirst(array(
				'conditions' => array('id'=>$id)
				));
			$d['id'] = $id;
		}
		$this->set($d);
	}

	/**
	* Permet de supprimer une catégorie
	**/
	function admin_delete($id){
		$this->loadModel('Categorie');
		$this->Categorie->delete($id);
		$this->Session->setFlash('Le contenu a bien été supprimé','alert-success');
		$this->redirect('admin/categories/index');
	}

}
?>